package com.chiaching.webmagic.service.impl;

import com.chiaching.webmagic.domain.bo.JdGoodSearchBO;
import com.chiaching.webmagic.domain.document.GoodDocument;
import com.chiaching.webmagic.repostory.service.GoodsRepostoryService;
import com.chiaching.webmagic.service.GoodsService;
import com.chiaching.webmagic.util.JdSearchUtils;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @email 1738870995@qq.com
 * @author: 何佳靖
 * @date: 2022/3/26 11:49
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsRepostoryService goodsRepostoryService;

    @Resource
    private JdSearchUtils jdSearchUtils;


    @Override
    public List<GoodDocument> searchGoodsForJdSaveToEs(String goodsName) {
        List<JdGoodSearchBO> search = jdSearchUtils.search(goodsName);
        goodsRepostoryService.addGoods(search.stream().map(e ->{
            GoodDocument goodDocument = new GoodDocument();
            goodDocument.setImg(e.getImg());
            goodDocument.setTitle(e.getTitle());
            goodDocument.setPrice(e.getPrice());
            goodDocument.setDesc(e.getDesc());
            goodDocument.setCreateTime(new Date());
            goodDocument.setUpdateTime(new Date());
            return goodDocument;
        }).collect(Collectors.toList()));
        return null;
    }
}
